package com.briup.myblog.web.controller;
import com.briup.myblog.aop.Logging;
import com.briup.myblog.bean.basic.BaseConfig;
import com.briup.myblog.service.IBaseConfigService;
import com.briup.myblog.utils.Result;
import com.briup.myblog.utils.ResultUtil;
import com.briup.myblog.web.vm.InserVM;
import com.briup.myblog.web.vm.UpdateVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import com.briup.myblog.bean.ext.Config;
@Api(tags = "系统配置模块")
@RestController
@RequestMapping("/auth/config")
public class ConfigController {
    @Autowired
    private IBaseConfigService service;
    @ApiOperation("查询信息")
    @Logging
    @GetMapping("/current/config/yes")
    public  Result currentConfigYes(HttpServletRequest request)throws Exception{
        BaseConfig baseConfig=service.currentConfigYes();
        return ResultUtil.success(baseConfig);
    }
    @ApiOperation("根据ID删除信息")
    @Logging
    @DeleteMapping("/deleteById")
    public Result deleteById(int id,HttpServletRequest request)throws Exception{
       service.deleteById(id);
       return  ResultUtil.success("删除成功");


    }
    @ApiOperation("保存信息")
    @Logging
    @PostMapping("/save")
    public  Result saveConfig(InserVM vm){
        BaseConfig config=new BaseConfig();
        config.setConfigIcon(vm.getConfigIcon());
        config.setConfigInfo(vm.getConfigInfo());
        config.setConfigName(vm.getConfigName());
        service.saveConfig(config);
        BeanUtils.copyProperties(vm,config);
        return ResultUtil.success("保存成功");

    }
    @ApiOperation("更新信息")
    @Logging
    @PutMapping("/update")
    public  Result updateConfig(UpdateVM vm){
        BaseConfig config=new BaseConfig();
        config.setConfigIcon(vm.getConfigIcon());
        config.setConfigInfo(vm.getConfigInfo());
        config.setConfigName(vm.getConfigName());
        config.setConfigId(vm.getConfigId());
        service.updateConfig(config);
        BeanUtils.copyProperties(vm,config);
        return ResultUtil.success("更新成功");
    }
    @ApiOperation("根据ID更新status信息")
    @Logging
    @PutMapping("/update/status")
    public  Result  changeConfigStatus(int id,int status){
         service.changeConfigStatus(id,status);
        return  ResultUtil.success("配置信息状态修改成功");
    }
    @ApiOperation("分页查询信息")
    @Logging
    @GetMapping("/page/query")
    public Result findAllWithPage(int pageNum, int pageSize,HttpServletRequest request)throws Exception{
             service.findAllWithPage(pageNum,pageSize);
             return ResultUtil.success(service.findAllWithPage(pageNum,pageSize));
    }
}
